package com.orangelife.mobile.verify.biz;

import android.os.Handler;

import com.curry.android.http.OrangeErrorHandler;
import com.curry.android.http.VolleyHttpUtil;
import com.curry.android.util.StringUtil;
import com.orangelife.mobile.constants.Constant;

import java.util.HashMap;

/**
 * 请求所有橙子生活的短信的业务的管理类
 * <p/>
 * 对应橙子生活的“数据分域” - “信息发送” - “短信验证码”
 * <p/>
 * <p/>
 * Created by Administrator on 2015/12/7.Modify by wgf on 16080801.
 */
public class SmsHttpBiz {

    /** 数据分域的信息发送的默认获取短信验证码的，不传默认为1 */
    private static final String USE_TYPE_DEFAULT = "1";
    private static final String USE_TYPE_LOGIN = "4";
    public static final int WHAT_SMS = 0;
    public static final int WHAT_VOICE_CODE = 66;


    public static void getSmsCode(String phone, VolleyHttpUtil.ResponseCallBack callback) {
        getSmsCode(phone, USE_TYPE_DEFAULT, false, null, callback, null, -1);
    }

    public static void getSmsCode(String phone, Handler handler, int what) {
        getSmsCode(phone, USE_TYPE_DEFAULT, handler, what);
    }

    public static void getSmsCode(String phone, String useType, Handler handler, int what) {
        getSmsCode(phone, useType, false, null, null, handler, what);
    }

    /** 请求发送短信验证码（登录） */
    public static void getSmsVerifyCode(String phone, boolean hasSentSmsVerify, int what, Handler handler) {
        getSmsCode(phone, USE_TYPE_LOGIN, hasSentSmsVerify, null, null, handler, what);
    }

    /**
     * 请求短信验证码
     *
     * @param phone            用户手机号
     * @param useType          默认为1，
     * @param hasSentSmsVerify 默认不传,如果特殊需求（登录）,需要后台短信运营商轮流切换发送,需要设置1。
     * @param useTime          默认不传，验证码有效时间(默认10分钟)
     * @param callback
     * @param handler
     * @param what
     */
    public static void getSmsCode(String phone, String useType, boolean hasSentSmsVerify, String useTime, VolleyHttpUtil.ResponseCallBack callback, Handler handler, int what) {
        //URL=http://10.168.8.231:8080/message_api/app/orangelife/service/sms/{phone}/new;
        HashMap<String, String> map = new HashMap<>();
        if (!StringUtil.isBlank(useType)) {/*new接口已废弃，不传*/
            map.put("useType", useType);
        }
        if (hasSentSmsVerify) {/*特殊情况需要用时，传1*/
            map.put("smsSendType", "1");
        }
        if (!StringUtil.isBlank(useTime)) {/*默认不传*/
            map.put("useTime", useTime);
        }
        map.put("{phone}", phone);
        if (callback != null) {
            VolleyHttpUtil.getInstance().getWithCallback(Constant.PATH_GET_LOGIN_CODE, map, callback);
        } else if (handler != null) {
            VolleyHttpUtil.getInstance().getWithHandler(Constant.PATH_GET_LOGIN_CODE, handler, what, map);
        }
    }

    public static boolean handleVerifyCode(Object response, String errInfo, String okInfo) {
        return OrangeErrorHandler.getInstance().isSuccess(response, errInfo, okInfo);
    }

    public static void getSmsCodeOld(String phone, String useType, Handler handler, int what) {
        HashMap<String, String> urlMap = new HashMap<>();
        urlMap.put("{phone}", phone);
        urlMap.put("useType", useType);
        VolleyHttpUtil.getInstance().getWithHandler(Constant.PATH_GET_CODE, handler, what, urlMap);
    }

    /** 请求发送语音验证码 */
    public static void getVoiceVerifyCode(String phone, int what, Handler handler) {
        HashMap<String, String> urlMap = new HashMap<>();
        urlMap.put("{phone}", phone);
        VolleyHttpUtil.getInstance().getWithHandler(Constant.PATH_VOICE_CODE, handler, what, urlMap);
    }

    /**
     * 请求发送语音验证码
     *
     * @param phone 手机
     */
    public static void getVoiceCode(String phone, Handler handler) {
        getVoiceVerifyCode(phone, WHAT_VOICE_CODE, handler);
    }

    public static void getVoiceCode(String phone, VolleyHttpUtil.ResponseCallBack callback) {
        HashMap<String, String> map = new HashMap<>();
        map.put("{phone}", phone);
        VolleyHttpUtil.getInstance().getWithCallback(Constant.PATH_VOICE_CODE, map, callback);
    }

    /**
     * 验证短信验证码是否正确(重置支付密码)
     *
     * @param phone    注册账号加密
     * @param code     接收的短信验证码
     * @param callback
     */
    public static void checkSmsVerifyCode(String phone, String code, VolleyHttpUtil.ResponseCallBack callback) {
        HashMap<String, String> map = new HashMap<>();
        map.put("memberName", StringUtil.md5(phone));
        map.put("verifyCode", code);
        if (callback != null) {
            VolleyHttpUtil.getInstance().getWithCallback(Constant.URL_VERIFY_SMS, map, callback);
        }
    }
}
